/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.sys.bean.RoleBean;
import com.rf.sys.dao.RoleMapper;
import com.rf.sys.dao.RoleResMapper;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.sys.model.Role;
import com.rf.sys.model.RoleExample;
import com.rf.sys.model.RoleRes;
import com.rf.sys.model.RoleResExample;
import com.rf.sys.service.RoleService;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.UuidUtil;

@Service("roleService")
public class RoleServiceImpl implements RoleService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private RoleMapper roleMapper;//注入dao
   	@Autowired
   	private RoleResMapper roleResMapper;

   	/**
	 * 根据id获取数据
	 * @param roleId
	 * @return Role
	 */
   	@Override
	public Role getRoleByID(String id) throws Exception{
		// TODO Auto-generated method stub
		return roleMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 根据Role获取数据
	 * @param Role
	 * @return List<Role>
	 */
	@Override
	public List<Role> getRole(Role role) throws Exception{
		// TODO Auto-generated method stub
		RoleExample example = new RoleExample();
		List<Role> list = roleMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<Role>
	 */
	@Override
	public List<Role> getRole() throws Exception{
		// TODO Auto-generated method stub
		RoleExample example = new RoleExample();
		return roleMapper.selectByExample(example);
	}
   	
	/**
	 * 保存信息
	 * @param Role
	 * @return
	 */
	@Override
	public void addRole(Role role) throws Exception{
		// TODO Auto-generated method stub
		role.setId(UuidUtil.get32UUID());
		roleMapper.insert(role);
	}

	/**
	 * 修改数据
	 * @param Role
	 * @param
	 */
	@Override
	public void updateRole(Role role) throws Exception{
		// TODO Auto-generated method stub
		roleMapper.updateByPrimaryKeySelective(role);
	}
	
	/**
	 * 删除数据
	 * @param roleId
	 * @param
	 */
	@Override
	public void deleteRole(String id) throws Exception{
		// TODO Auto-generated method stub
		roleMapper.deleteByPrimaryKey(id);
	}
	
	/**
	 * 删除数据
	 * @param roleIds
	 * @param
	 */
	@Override
	public void deleteRoleState(String ids) throws Exception{
		// TODO Auto-generated method stub
		String sql = "update sys_role set state = "+ConfigUtils.DELSTATE +" where id in("+ids+")";
		sqlAdapterMapper.updateSQL(sql);
	}
	
	/**
	 * 分配菜单
	 * @param roleid
	 * @param resid
	 * @param
	 */
	@Override
	public void allot(String roleid,String resid) throws Exception{
		//删除该角色下的所有菜单
		RoleResExample example = new RoleResExample();
		example.createCriteria().andRoleIdEqualTo(roleid);
		roleResMapper.deleteByExample(example);
		
		String[] resids = resid.split(",");
		for(int i=0;i<resids.length;i++){
			RoleRes record = new RoleRes();
			record.setId(UuidUtil.get32UUID());
			record.setRoleId(roleid);
			record.setResId(resids[i]);
			roleResMapper.insert(record);
		}
	}

	/**
	 * 获取数据分页
	 * @param Role
	 * @param limit
	 * @param offset
	 * @return Map
	 */
	@Override
	public Map getRolePage(Role role,PageUtils pageUtils) throws Exception{
		RoleExample example = new RoleExample();
		com.rf.sys.model.RoleExample.Criteria criteria= example.createCriteria();
		if(role.getName() !=null && !role.getName().equals("")){
			criteria.andNameLike("%"+role.getName()+"%");
		}
		if(role.getState()!=null && !role.getState().equals("")){
			criteria.andStateEqualTo(role.getState());
		}
		criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//用户状态不等于删除
		
		//获取第page页，每页共rows条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<Role> list = roleMapper.selectByExample(example);
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", list);
		return dateMap;
	}
	
	/**
	 * 获取数据分页--用于用户页分配角色
	 * @param id  用户ID
	 * @param Role
	 * @param limit
	 * @param offset
	 * @return Map
	 */
	@Override
	public Map getRolePageForUser(String uid,Role role,PageUtils pageUtils) throws Exception{
		String sql = "select r.*,ur.user_id from sys_role r LEFT JOIN sys_user_role ur on "
				+ " r.id = ur.role_id and ur.user_id = '"+uid+"' where r.state = '100101' ";
		if(role != null){
			if(role.getName() != null && !role.getName().equals("")){
				sql = sql + " and r.name like '%"+role.getName()+"%'";
			}
		}
		sql = sql + " ORDER BY r.id desc ";
		//获取第page页，每页共rows条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<Map<String, Object>> list =sqlAdapterMapper.selectSQL(sql);
		List<RoleBean> rList = new ArrayList<RoleBean>();
		for(int i=0;i<list.size();i++){
			RoleBean rb = new RoleBean();
			Map<String, Object> map = (Map<String, Object>)list.get(i);
			rb.setId((String)map.get("ID"));
			rb.setName((String)map.get("NAME"));
			rb.setDescription((String)map.get("DESCRIPTION"));
			rb.setRoleKey((String)map.get("ROLE_KEY"));
			rb.setState((String)map.get("STATE"));
			String user_id = (String)map.get("USER_ID");
			if(user_id == null || user_id.equals("")){
				rb.setUid("");
			}else{
				rb.setUid(user_id);
			}
			rList.add(rb);
		}
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", rList);
		return dateMap;
	}
}

